﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Bee.Util;

namespace Bee.BLL
{
    public class EmployeeInfoManager
    {
        private static readonly EmployeeInfoManager instance = new EmployeeInfoManager();

        private EmployeeInfoManager()
        {
        }

        public static EmployeeInfoManager Instance
        {
            get
            {
                return instance;
            }
        }

        public DataTable EmployeeOrgLookup()
        {
            DataTable orgData = DataMapping.Instance.GetMapping("OrganizationMapping") as DataTable;

            DataTable employeeData = DataMapping.Instance.GetMapping("EmployeeMapping") as DataTable;

            DataTable employeeOrgData = new DataTable();
            employeeOrgData.Columns.Add("id");
            employeeOrgData.Columns.Add("name");
            employeeOrgData.Columns.Add("parentid");

            foreach (DataRow item in orgData.Rows)
            {
                DataRow newRow = employeeOrgData.NewRow();

                newRow["id"] = "o{0}".FormatWith(item["id"]);
                newRow["name"] = item["name"];
                newRow["parentid"] = "o{0}".FormatWith(item["parentid"]);

                employeeOrgData.Rows.Add(newRow);
            }

            foreach (DataRow item in employeeData.Rows)
            {
                DataRow newRow = employeeOrgData.NewRow();

                newRow["id"] = "e{0}".FormatWith(item["id"]);
                newRow["name"] = item["truename"];
                newRow["parentid"] = "o{0}".FormatWith(item["departmentid"]);

                employeeOrgData.Rows.Add(newRow);
            }

            return employeeOrgData;
        }

        public int GetEmployeeOrg(int employeeId)
        {
            int result = 0;
            DataTable employeeData = DataMapping.Instance.GetMapping("EmployeeMapping") as DataTable;

            DataRow[] rows = employeeData.Select("id=" + employeeId);
            if (rows.Length == 1)
            {
                result = (int)rows[0]["departmentid"];
            }

            return result;
        }

        public int GetOrgLeader(int orgId)
        {
            int result = 0;
            DataTable orgData = DataMapping.Instance.GetMapping("OrganizationMapping") as DataTable;

            DataRow[] rows = orgData.Select("id=" + orgId);
            if (rows.Length == 1)
            {
                result = int.Parse(rows[0]["owneremployeeid"].ToString());
            }

            return result;
        }

        public int GetEmployeeLeader(int employeeId)
        {
            int result = 0;
            int departmentId = GetEmployeeOrg(employeeId);
            if (departmentId > 0)
            {
                result = GetOrgLeader(departmentId);
            }

            return result;
        }
    }
}
